﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmImport : DevExpress.XtraEditors.XtraForm
    {
        public frmImport()
        {
            InitializeComponent();
        }

        private void frmImport_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable objDanhSach;

                objDanhSach = clsDbAccess.fncGetNhapHang(Program.gobjDb);

                grdControlDanhSach.DataSource = objDanhSach;

                if (objDanhSach != null)
                {
                    if (objDanhSach.Rows.Count > 0)
                    {
                        dgvDanhSach.FocusedRowHandle = 0;
                        grdControlDanhSach_Click(null, null);
                    }
                }

                //hide price
                if (!Program.gblnAdminLoggedIn)
                {
                    colDon_gia.Visible = false;
                    colTotal.Visible = false;
                }

                Program.gevnApplication.evnNhapHangUpdate += gevnApplication_evnNhapHangUpdate;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        void gevnApplication_evnNhapHangUpdate()
        {
            try
            {
                DataTable objDanhSach;

                objDanhSach = clsDbAccess.fncGetNhapHang(Program.gobjDb);

                grdControlDanhSach.DataSource = objDanhSach;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void grdControlDanhSach_Click(object sender, EventArgs e)
        {
            try
            {
                int intRow = dgvDanhSach.FocusedRowHandle;

                if (intRow < 0) return;

                int intNhapHangID = clsCommon.fncCnvNullToInt(dgvDanhSach.GetRowCellValue(intRow, colNhaphang_id));
                DateTime dtNgayNhap = clsCommon.fncCnvNullToDatetime(dgvDanhSach.GetRowCellValue(intRow, colNhaphang_date));
                string strNguoiNhap = clsCommon.fncCnvNullToString(dgvDanhSach.GetRowCellValue(intRow, colTennhanvien));
                string strGhiChu = clsCommon.fncCnvNullToString(dgvDanhSach.GetRowCellValue(intRow, colNhaphang_ghichu));
                int intSoLuong = clsCommon.fncCnvNullToInt(dgvDanhSach.GetRowCellValue(intRow, colNhaphang_soluong));

                dtNhapHang.EditValue = dtNgayNhap;
                txtNguoiNhap.Text = strNguoiNhap;
                txtGhiChu.Text = strGhiChu;
                txtSoLuong.Text = intSoLuong.ToString();

                DataTable objSanPham = clsDbAccess.fncGetNhapHangChiTiet(Program.gobjDb, intNhapHangID);
                grdControlSanPham.DataSource = objSanPham;

                if (objSanPham == null) return;
                for (int i = 0; i <= objSanPham.Rows.Count - 1; i++)
                {
                    int intSL = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colSo_luong));
                    int intPrice = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colDon_gia));
                    dgvSanPham.SetRowCellValue(i, colTotal, intSL * intPrice);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnNewImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                try
                {
                    SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                    frmImportNew frmNewImport = new frmImportNew();

                    //check
                    foreach (Form form in Application.OpenForms)
                    {
                        if (form.GetType() == frmNewImport.GetType())
                        {
                            frmNewImport.Activate();
                            return;
                        }
                    }

                    frmNewImport.MdiParent = this.MdiParent;
                    frmNewImport.Show();
                }
                catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
                finally
                {
                    SplashScreenManager.CloseForm(false);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

    }
}